#define _GNU_SOURCE
#include"../include/workque.h"
#include "../include/thread_Pool.h"
#include "../include/head.h"
#include<fcntl.h>
void sigfunc(int signum)
{
    printf("sig is comming\n");
}

int transFile(int clienFd,char *filename)
{

    signal(SIGPIPE, sigfunc);
    //使用私有协议发送数据，人为的规定发送数据的边界
    Train_t train;
    
    
	

    memset(&train, 0, sizeof(train));
    int fd = open(filename, O_RDWR);
    if(fd!=-1)
    {
        printf("打开文件成功\n");
    }
    else
    {
        printf("打开文件失败\n");
    }
    //存储文件名长度
    train.len = 4;
    strcpy(train.buf, "file");

    //发送文件名
    int ret1 = send(clienFd, &train, 4 + train.len, 0);
    printf(" ret1 = %d\n",ret1);
    
    struct stat fileInfo;
    bzero(&fileInfo, sizeof(fileInfo));

    
    fstat(fd, &fileInfo);

    train.len = sizeof(fileInfo.st_size);
    memcpy(train.buf, &fileInfo.st_size, train.len);

    //发送文件的总长度
    ret1 = send(clienFd, &train, 4 + train.len, 0);
    printf("filesize = %ld\n", fileInfo.st_size);

    printf("ret 1 filesize %d\n",ret1);


    
    int sfd[2];
    pipe(sfd);
    
    int recvLen = 0;
    while(recvLen < fileInfo.st_size)
    {
        int ret = splice(fd, 0, sfd[1], 0, 4096, 0);
        ret = splice(sfd[0], 0, clienFd, 0, ret, 0);
        recvLen += ret;
    }

    
    return 0;
}

